<?php

namespace  Ekadmin\Controller;
use \Think\Controller;


class LoginController extends Controller
{
        public  function index()
        {


            if(checkAuth()){
                $this->redirect('Index/index');
            }
            $this->display('login');
        }



        public function dologin()
        {
            if(IS_POST){
                $param = I('post.');

               if(!$this->check_verify($param['verify'],'login')){
                   ajax_return(1,'验证码错误');
               }


                $user = M('admin')->where(['username'=>$param['username']])->find();
                if($user){
                    if($user['password']!=password($param['password'])){
                        ajax_return(1,'密码错误');
                    }

                    $salt = C("COOKIE_SALT");
                    $ip = get_client_ip();
                    session_start();
                    session('uid',$user['id']);                                        session('username',$user['username']);
                    //加密cookie信息                    
                    $auth = password($user['id'].$user['username'].$ip.$salt);
                    cookie('auth', $auth);
                    ajax_return(0,'登录成功',U('index/index'));

                }else{
                    ajax_return(1,'账号不存在');
                }
            }
        }



    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function verify()
    {
        $config = array(
            'fontSize' => 14, // 验证码字体大小
            'length' => 4, // 验证码位数
            'useNoise' => false, // 关闭验证码杂点
            'imageW' => 100,
            'imageH' => 30,
        );
        $verify = new \Think\Verify($config);
        $verify->entry('login');
    }

    public function logout()
    {
        cookie('auth', null);
        session('uid',null);
        $url = U("login/index");
        header("Location: {$url}");
        exit(0);
    }


}